package thread;
/**互斥性
* 当使用多个synchronized锁定多个代码片段并且指定的同步监视器对象是同一个时,这些代码片段之间就是互斥的*/
public class SyncDemo4 {
    public static void main(String[] args) {
        Boo boo = new Boo();
        Thread t1 = new Thread(()-> boo.doSome1());
        Thread t2 = new Thread(()-> boo.doSome2());
        t1.start();
        t2.start();
    }
}
class Boo{
    public synchronized void doSome1(){
        Thread t1 = Thread.currentThread();
        System.out.println("1开始了");
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("1结束了");
    }
    public void doSome2(){
        synchronized (this) {
            Thread t1 = Thread.currentThread();
            System.out.println("2开始了");
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("2结束了");
        }
    }
}
